package com.ow.dva.util.lockSocket;

import com.alibaba.fastjson.JSONArray;
import com.ow.dva.module.order.entity.Order;
import com.ow.dva.module.order.service.OrderService;
import com.ow.dva.module.park.entity.Park;
import com.ow.dva.module.park.entity.ParkArea;
import com.ow.dva.module.park.entity.Parkinglot;
import com.ow.dva.module.park.entity.Road;
import com.ow.dva.module.park.service.ParkAreaService;
import com.ow.dva.module.park.service.ParkService;
import com.ow.dva.module.park.service.ParkinglotService;
import com.ow.dva.module.park.service.RoadService;
import com.ow.dva.module.passingRecord.entity.PassingRecord;
import com.ow.dva.module.passingRecord.service.PassingRecordService;
import com.ow.dva.module.terminal.entity.Termina;
import com.ow.dva.module.terminal.service.TerminaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
public class HandleSocket {

    private static HandleSocket staticInstance;

    @Autowired
    private PassingRecordService passingRecordService;

    @Autowired
    private ParkService parkService;

    @Autowired
    private ParkinglotService parkinglotService;

    @Autowired
    private TerminaService terminaService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private RoadService roadService;

    @Autowired
    private ParkAreaService parkAreaService;

    @PostConstruct
    public void init() {
        staticInstance = this;
        staticInstance.passingRecordService = this.passingRecordService;
        staticInstance.terminaService = this.terminaService;
        staticInstance.orderService = this.orderService;
        staticInstance.roadService = this.roadService;
        staticInstance.parkAreaService = this.parkAreaService;
    }

    /**
     * @author: 姜延鹏
     * @Description: 保存上传的过车记录（socket模式）
     * @date 2020/3/18
     * @return
     */
    public static void savePassRec(String passing) {
//        获取数据并转为实体List
        JSONArray passingJson = JSONArray.parseArray(passing);
//        当天所有的过车记录
        List<PassingRecord> passingRecords = passingJson.toJavaList(PassingRecord.class);
//        跳过已存在的停车记录，保存不存在的
        if (passingRecords.size() > 0) {
            for (PassingRecord p : passingRecords) {
                PassingRecord passingRecord = staticInstance.passingRecordService.getById(p.getId());
                if (passingRecord != null){
                    staticInstance.passingRecordService.updateById(p);
                }else {
                    staticInstance.passingRecordService.save(p);
                }
            }
        }

    }

    /**
     * @author 姜延鹏
     * @Description 保存获取的过车记录（post模式）
     * @param passingRecords
     * @date 2020/3/27
     * @return void
     */
    public static void savePassingRecord(List<PassingRecord> passingRecords) {
//        跳过已存在的停车记录，保存不存在的
        if (passingRecords.size() > 0) {
            for (PassingRecord p : passingRecords) {
                PassingRecord passingRecord = staticInstance.passingRecordService.getById(p.getId());
                if (passingRecord != null){
                    staticInstance.passingRecordService.updateById(passingRecord);
                }else {
                    staticInstance.passingRecordService.save(p);
                }
            }
        }

    }

    /**
     * @author: 姜延鹏
     * @Description: 保存更新上传的停车场信息
     * @date 2020/3/18
     * @return
     */
    public static void savePark(List<Park> parks,String terminalCode,String terminalId) {
        try {
//            JSONArray passingJson = JSONArray.parseArray(passing);
//            List<Park> parks = passingJson.toJavaList(Park.class);
//            跳过已存在的停车场，保存不存在的
            for (Park park : parks) {
                park.setTerminaCode(terminalCode);
                park.setTerminaId(terminalId);
                Park p = staticInstance.parkService.getById(park.getId());
                if (p != null) {
                    staticInstance.parkService.updateById(park);
                } else {
                    staticInstance.parkService.save(park);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author: 姜延鹏
     * @Description: 保存更新上传的车位信息
     * @date 2020/3/18
     * @return
     */
    public static void saveParkinglot(List<Parkinglot> parkinglots) {
        try {
//            JSONArray passingJson = JSONArray.parseArray(passing);
//            List<Parkinglot> parkinglots = passingJson.toJavaList(Parkinglot.class);
//            跳过已存在的车位，保存不存在的
            for (Parkinglot p : parkinglots) {
                Parkinglot parkinglot = staticInstance.parkinglotService.getById(p.getId());
                if (parkinglot != null) {
                    staticInstance.parkinglotService.updateById(p);
                } else {
                    staticInstance.parkinglotService.save(p);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
   /**
     * @author: 姜延鹏
     * @Description: 保存更新上传的车道信息
     * @date 2020/3/18
     * @return
     */
    public static void saveRoad(List<Road> roads) {
        try {
            for (Road r : roads) {
                Road road = staticInstance.roadService.getById(r.getId());
                if (road != null) {
                    staticInstance.roadService.updateById(r);
                } else {
                    staticInstance.roadService.save(r);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
   /**
     * @author: 姜延鹏
     * @Description: 保存更新上传的停车区域信息
     * @date 2020/3/18
     * @return
     */
    public static void saveParkAreas(List<ParkArea> parkAreas) {
        try {
            for (ParkArea p : parkAreas) {
                ParkArea parkArea = staticInstance.parkAreaService.getById(p.getId());
                if (parkArea != null) {
                    staticInstance.parkAreaService.updateById(p);
                } else {
                    staticInstance.parkAreaService.save(p);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @author 姜延鹏
     * @Description 保存绑定的终端机信息
     * @param termina
     * @date 2020/3/25
     * @return void
     */

    public static void saveServerSettings(Termina termina) {
        try {

            staticInstance.terminaService.save(termina);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void deleteByTerminaId(Termina termina) {
        try {

            staticInstance.terminaService.removeById(termina);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void editByTermianId(Termina termina) {
        try {

            staticInstance.terminaService.updateById(termina);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @author 姜延鹏
     * @Description 保存上传的订单
     * @param orders
     * @date 2020/3/26
     * @return void
     */
    public static void saveOrder(List<Order> orders) {
        try {

            for (Order order : orders){
                Order o = staticInstance.orderService.getById(order.getId());
                if (o != null){
                    if (o.getOrderStatus() != 0){
                        staticInstance.orderService.updateById(order);
                    }else {
                        continue;
                    }
                }else {
                    staticInstance.orderService.save(order);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
